//
//  EditAddressViewController.swift
//  Box
//
//  Created by mn on 2025/3/31.
//

import UIKit

class EditAddressViewController: BaseViewController {
    var data: AddressData?
    let editView = EditAddressView()
    let regionView = RegionView()
    lazy var button: InsetBorderButton = {
        let button = InsetBorderButton(type: .custom)
        button.setTitle("保存", for: .normal)
        button.setTitleColor(.fromHex("#CAFE67"), for: .normal)
        button.layer.cornerRadius = 7.5
        button.titleLabel?.font = .systemFont(ofSize: 16)
        button.backgroundColor = .black
        button.borderColor = .white
        button.addAction { [weak self] in
            self?.addAction()
        }
        return button
    }()
    init(data: AddressData? = nil) {
        self.data = data
        super.init(nibName: nil, bundle: nil)
        self.title = data == nil ? "新增地址" : "编辑地址"
        self.addressPath = data?.addressPath
    }
    var addressPath: String? = nil
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        let bg = UIView()
        bg.backgroundColor = .fromHex("#F4F6F8")
        view.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.top.equalTo(nav.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }

        view.addSubview(editView)
        editView.snp.makeConstraints { make in
            make.top.equalTo(nav.snp.bottom).offset(16)
            make.left.right.equalToSuperview().inset(16)
            make.height.equalTo(250)
        }
        editView.tapSelectRegion = {
            [weak self] in
            if $0 {
                self?.showRegionView()
            }else{
                self?.regionView.removeFromSuperview()
            }
            
        }
        view.addSubview(button)
        button.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(46.5)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-24)
            make.height.equalTo(52)
        }
        regionView.doneRegion = {
            [weak self] (desc,code) in
            self?.editView.regionTextField.text = desc
            self?.addressPath = code
        }
        getNewRegionInfo()
        guard let data else { return  }
        editView.data = data
    }
    func showRegionView() {
        self.view.endEditing(true)
        view.addSubview(regionView)
        regionView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalToSuperview().multipliedBy(0.4)
        }
    }
    func addAction() {
        guard let name = editView.nameTextField.text,!name.isEmpty else {
            toast("请输入姓名")
            return
        }
        guard let mobile = editView.phoneTextField.text,!mobile.isEmpty else {
            toast("请输入电话")
            return
        }
        guard let addressPath else {
            toast("请选择省市区")
            return
        }
        guard let detailAddress = editView.addressTextField.text,!detailAddress.isEmpty else {
            toast("请输入详细地址")
            return
        }
        if let data{
            APIService.request(api: .updateAddress(id: data.id,
                                                   addressPath: addressPath,
                                                   detailAddress: detailAddress,
                                                   mobile: mobile,
                                                   name: name,
                                                   defaultSelected: editView.defaultButton.isSelected ? 1: 0))
                                                {
                [weak self] (result,_) in
                if result.success{
                    self?.navigationController?.pop()
                }
            }
        }else{
            APIService.request(api: .addAddress(addressPath: addressPath,
                                                detailAddress: detailAddress,
                                                mobile: mobile,
                                                name: name,
                                                defaultSelected: editView.defaultButton.isSelected ? 1: 0)) {
                [weak self] (result,_) in
                if result.success{
                    self?.navigationController?.pop()
                }
            }
        }
        
    }
    func getNewRegionInfo() {
        APIService.request(api: .getNewRegionInfo) {
            [weak self] (result,response) in
            if result.success ,let list: RegionInfoData = Decoder.data(response){
                self?.regionView.provinces = list.data
            } else {
                self?.toast(result.message)
            }
        }
    }
}
